package org.servlet;

import com.sun.xml.internal.ws.api.ha.StickyFeature;
import org.apache.log4j.Logger;
import org.domain.Account;
import org.service.AccountService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RegisterServlet extends HttpServlet {
    private static final String REGISTER="/WEB-INF/JSP/account/NewAccountForm.jsp";
    private AccountService accountService;
    private static Logger logger = Logger.getLogger(Class.class);
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String repeatedPassword = request.getParameter("repeatedPassword");
        String firstName = request.getParameter("firstName");
        String lastName = request.getParameter("lastName");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        String address1 = request.getParameter("address1");
        String address2 = request.getParameter("address2");
        String city = request.getParameter("city");
        String state = request.getParameter("state");
        String zip = request.getParameter("zip");
        String country = request.getParameter("country");
        String languagePreference = request.getParameter("languagePreference");
        String favoriteCategory = request.getParameter("favoriteCategory");
        boolean listOpt = !(request.getParameter("listOpt")==null);
        boolean bannerOpt = !(request.getParameter("bannerOpt")==null);
        Account account = new Account();
        account.setUsername(username);
        account.setPassword(password);
        account.setFirstName(firstName);
        account.setLastName(lastName);
        account.setEmail(email);
        account.setPhone(phone);
        account.setAddress1(address1);
        account.setAddress2(address2);
        account.setCity(city);
        account.setState(state);
        account.setZip(zip);
        account.setCountry(country);
        account.setLanguagePreference(languagePreference);
        account.setFavouriteCategoryId(favoriteCategory);
        account.setListOption(listOpt);
        account.setBannerOption(bannerOpt);
        if(!password.equals(repeatedPassword)){
            request.setAttribute("msg", "Unmatched password!");
            request.getRequestDispatcher(REGISTER).forward(request, response);
        }else {
            accountService = new AccountService();
            accountService.insertAccount(account);
            logger.info(String.format("New Account (%s) Created.", account.getUsername()));
        }
        response.sendRedirect("main");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.getRequestDispatcher(REGISTER).forward(request,response);
    }
}
